package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.common.PageRequest;
import com.kaili.common.util.CommonRegexUtil;
import com.kaili.db.service.IHsBasicSeqService;
import com.kaili.db.service.IHsOptEwbAppointService;
import com.kaili.pojo.dto.appoint.EwbAppointAddDTO;
import com.kaili.pojo.dto.appoint.EwbAppointEditDTO;
import com.kaili.pojo.dto.appoint.EwbAppointQueryDTO;
import com.kaili.pojo.vo.AppointQueryVO;
import com.kaili.common.exception.BaseExcept;
import com.kaili.except.ResponseStatus;
import com.kaili.utils.DateUtils;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "预约登记服务", description = "预约登记服务相关API")
@Validated
public class AppointController {
    @Autowired
    IHsOptEwbAppointService ewbAppointService;
    @Autowired
    IHsBasicSeqService seqService;

    @Autowired
    ILoginUserInfoService loginUserInfoService;

    @ApiOperation(value = "获取预约号")
    @GetMapping("/appoints/appointCode")
    public String setAppointCode() {
        return seqService.seqByTypeWithType("YY");
    }

    @PostMapping(value = "/appoints")
    @ApiOperation(value = "新增预约登记", notes = "新增预约登记")
    public ResponseStatus doAddEwbAppoint(@Validated @RequestBody EwbAppointAddDTO addDTO) {
        if(!CommonRegexUtil.isRegexWeight(addDTO.getTotalWeight())
                || !CommonRegexUtil.isRegexVal(addDTO.getTotalVol())){
            return com.kaili.except.ResponseStatus.APPOINT_ADD_ERR;
        }
        int ret = ewbAppointService.addEwbAppoint(addDTO);
        if (ret != 0) {
            return com.kaili.except.ResponseStatus.APPOINT_ADD_ERR;
        }
        return com.kaili.except.ResponseStatus.RESPONSE_OK;
    }

    @GetMapping(value = "/appoints")
    @ApiOperation(value = "分页查询预约登记数据列表")
    Page<AppointQueryVO> queryEwbAppointPageList(@Validated @ModelAttribute EwbAppointQueryDTO reqDTO) throws BaseExcept {
        if(reqDTO.getCur()!=null&&reqDTO.getPageSize()!=null){
            Map<Object,Object> map=new HashMap<>();
            map.putAll(new BeanMap(reqDTO));
            map.put("siteId",loginUserInfoService.getLoginSiteId());
            Page<AppointQueryVO> pager = new Page<AppointQueryVO>(reqDTO.getCur(), reqDTO.getPageSize());
            pager = ewbAppointService.queryEwbAppointPageList(pager,map);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.APPOINT_QUERY_ERR);
    }


    @PutMapping(value="/appoints/{appointNo}")
    @ApiOperation(value = "修改并保存预约登记", notes = "修改并保存预约登记")
    public ResponseStatus doEditEwbAppoint(@Validated @RequestBody EwbAppointEditDTO editDTO) {
        int ret = ewbAppointService.editEwbAppoint(editDTO);
        if (ret != 0) {
            return com.kaili.except.ResponseStatus.APPOINT_MODIFY_ERR;
        }
        return com.kaili.except.ResponseStatus.RESPONSE_OK;
    }


    @DeleteMapping(value = "/appoints/{appointNo}")
    @ApiOperation(value = "删除预约登记(单个)", notes = "删除预约登记(单个)")
    public ResponseStatus doDeleteEwbAppoint(@Pattern(regexp = "^[A-Za-z0-9]{0,30}$",message = "预约号应为[A-Za-z0-9]组成的1-30位字符")
                                             @PathVariable("appointNo") String appointNo) {
        int ret = ewbAppointService.deleteEwbAppoint(appointNo);
        if (ret != 0) {
            return com.kaili.except.ResponseStatus.APPOINT_DELETE_ERR;
        }
        return com.kaili.except.ResponseStatus.RESPONSE_OK;
    }

}
